package emulator.hardware.nmos6502.commands;

import emulator.EmulatorException;
import emulator.hardware.HwByte;
import emulator.hardware.nmos6502.Command;
import emulator.hardware.nmos6502.CommandSet;
import emulator.hardware.nmos6502.Cpu6502;
import emulator.hardware.nmos6502.Flags6502;
import emulator.hardware.nmos6502.Operand;
import emulator.hardware.nmos6502.operands.*;

/** LDX (LoaD X register)
 *  
 *  Affects Flags: S Z
 *  
 *  MODE           SYNTAX       HEX LEN TIM
 *  Immediate     LDX #$44      $A2  2   2
 *  Zero Page     LDX $44       $A6  2   3
 *  Zero Page,Y   LDX $44,Y     $B6  2   4
 *  Absolute      LDX $4400     $AE  3   4
 *  Absolute,Y    LDX $4400,Y   $BE  3   4+
 *  
 *  + add 1 cycle if page boundary crossed
 */
public class LDX implements Command {

	@Override
	public void execute(Cpu6502 cpu, Operand operand) throws EmulatorException {
		HwByte value = operand.getByte();
		Flags6502 flags = cpu.getFlags();
		flags.setNZFromValue(value);
		cpu.setX(value);
		cpu.setFlags(flags);
	}

	@Override
	public String getName() {
		return "LDX";
	}

	@Override
	public void register(CommandSet cset) {
		 cset.defineCommand(0xA2, this, new Immediate());
		 cset.defineCommand(0xA6, this, new ZeroPage());
		 cset.defineCommand(0xB6, this, new ZeroPageY());
		 cset.defineCommand(0xAE, this, new Absolute());
		 cset.defineCommand(0xBE, this, new AbsoluteY());
	}

}
